package cn.chenhao.pet.service.impl;

import cn.chenhao.basic.service.impl.BasicServiceImpl;
import cn.chenhao.basic.util.CodeGenerateUtils;
import cn.chenhao.basic.util.DistanceUtil;
import cn.chenhao.basic.util.PageList;
import cn.chenhao.basic.util.Point;
import cn.chenhao.order.domain.PetAcquisitionOrder;
import cn.chenhao.order.mapper.PetAcquisitionOrderMapper;
import cn.chenhao.org.domain.Employee;
import cn.chenhao.org.domain.Shop;
import cn.chenhao.org.mapper.EmployeeMapper;
import cn.chenhao.org.mapper.ShopMapper;
import cn.chenhao.pay.constants.PayConstants;
import cn.chenhao.pay.domain.PayBill;
import cn.chenhao.pet.domain.Pet;
import cn.chenhao.pet.domain.SearchMasterMsg;
import cn.chenhao.pet.mapper.SearchMasterMsgMapper;
import cn.chenhao.pet.query.SearchMasterMsgQuery;
import cn.chenhao.pet.service.IPetService;
import cn.chenhao.pet.service.ISearchMasterMsgService;
import cn.chenhao.user.domain.User;
import cn.chenhao.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class SearchMasterMsgServiceImpl extends BasicServiceImpl<SearchMasterMsg> implements ISearchMasterMsgService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IPetService petService;

    @Autowired
    private PetAcquisitionOrderMapper orderMapper;


    /**
     * 发布宠物寻主
     * @param msg
     * @param logininfoId
     */
    @Override
    public void publish(SearchMasterMsg msg, Long logininfoId) {
        // 1.发布寻主前，必须先登录，然后从user表中获取当前登录人的logininfo_id
        User user = userMapper.loadByLogininfoId(logininfoId);
        msg.setUser_id(user.getId());

        // 2.根据当前用户位置，获取最近的店铺
        Point point = DistanceUtil.getPoint(msg.getAddress());// 获取当前用户地址
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.loadAll());// 查询最近的店铺
        msg.setShop(nearestShop);// 保存最近的店铺
        msg.setShop_id(nearestShop.getId());// 将最近的店铺id保存到寻主表中
        searchMasterMsgMapper.save(msg);// 保存
    }

    /**
     * 待处理
     * @param msgQuery
     * @param logininfoId
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> pending(SearchMasterMsgQuery msgQuery, Long logininfoId) {
        Employee emp =  employeeMapper.loadByLogininfoId(logininfoId);
        if(emp.getShop_id() != null){
            msgQuery.setShopId(emp.getShop_id());
        }
        msgQuery.setState(0);// 0:待处理，1：已处理
        return super.queryPage(msgQuery);
    }

    /**
     * 已处理
     * @param msgQuery
     * @param logininfoId
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> processed(SearchMasterMsgQuery msgQuery, Long logininfoId) {
        Employee emp =  employeeMapper.loadByLogininfoId(logininfoId);
        if(emp.getShop_id() != null){
            msgQuery.setShopId(emp.getShop_id());
        }
        msgQuery.setState(1);// 0:待处理，1：已处理
        return super.queryPage(msgQuery);
    }

    /**
     * 个人中心
     * @param msgQuery
     * @param logininfoId
     * @return
     */
    @Override
    public PageList<SearchMasterMsg> queryUserMsg(SearchMasterMsgQuery msgQuery, Long logininfoId) {
        User user = userMapper.loadByLogininfoId(logininfoId);
        msgQuery.setUserId(user.getId());
        return super.queryPage(msgQuery);
    }

    /**
     * 处理寻主消息，携带了发布消息用户的id
     * @param pet
     * @param id
     */
    @Override
    public void handle(Pet pet, Long id) {
        // 通过获取详情页id去修改
        Long msgId = pet.getSearch_master_msg_id();
        // 通过id修改状态
        searchMasterMsgMapper.updateStateById(msgId);
        // 将修改后的寻主消息生成一条宠物信息
        petService.add(pet);

        // 生成宠物订单
        SearchMasterMsg masterMsg = searchMasterMsgMapper.loadById(pet.getSearch_master_msg_id());
        Employee emp = employeeMapper.loadByLogininfoId(id);
        PetAcquisitionOrder order = pet2order(pet, masterMsg, emp.getId());
        orderMapper.save(order);

        if(order.getPrice().longValue() > 0){
            System.out.println("下单成功！支付金额是：" + order.getPrice());
        }

    }



    private PetAcquisitionOrder pet2order(Pet pet, SearchMasterMsg adopt,Long empId) {
        PetAcquisitionOrder order = new PetAcquisitionOrder();
        order.setDigest("[摘要]对"+pet.getName()+"收购订单！");
        order.setState(0);//待支付
        order.setPrice(pet.getCostprice());
        order.setAddress(adopt.getAddress());
        String orderSn = CodeGenerateUtils.generateOrderSn(adopt.getUser_id());
        order.setOrderSn(orderSn);
        order.setPet_id(pet.getId());
        order.setUser_id(adopt.getUser_id());
        order.setPaytype(0);
        order.setShop_id(pet.getShop_id());
        order.setEmp_id(empId);
        return order;
    }
}
